package com.rc.saas.tenant.controller.elec;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.device.RcLabel;
import com.rc.saas.tenant.model.device.RcLabelExample;
import com.rc.saas.tenant.model.elec.ElecPile;
import com.rc.saas.tenant.model.tenant.*;
import com.rc.saas.tenant.search.elec.ElecLabelSearch;
import com.rc.saas.tenant.search.elec.ElecPileSearch;
import com.rc.saas.tenant.service.elec.ElecLabelService;
import com.rc.saas.tenant.service.tenant.TenantCompanyInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.device.RcLabelVo;
import com.rc.saas.tenant.vo.elec.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * zhoushiyu
 * 站标签
 */
@Controller
@RequestMapping("/elecLabel")
public class ElecLabelController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(ElecLabelController.class);


    @Autowired
    private TenantCompanyInfoService tenantCompanyInfoService;

    @Autowired
    private ElecLabelService elecLabelService;


    /**
     * 跳转站标签页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String elecStationPage(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        List<TenantCompanyInfo> tenantCompanyInfos = tenantCompanyInfoService.listByTenantCode(user.getTenantCode());
        model.addAttribute("tenantCompanyInfos", tenantCompanyInfos);
        return "/templates/elec/elecLabel";
    }

    /**
     * 返回桩群列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listLabel", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listLabel(ElecLabelSearch elecLabelSearch) {
        //默认其他省为10000
       /* if ("10000".equals(elecSationSearchVo.getAreaId())) {
            TenantUser user = ShiroUtils.getSessionUser();
            String provinceId = elecStationService.selectMostProvinceId(user.getTenantCode());
            elecSationSearchVo.setAreaId(null);
            elecSationSearchVo.setProvinceId(provinceId);
        }*/
        //PageInfo<ElecStationResult> pageInfo = elecStationService.listPageByCondition(elecSationSearchVo);
        PageInfo<RcLabelVo> pageInfo = elecLabelService.listPageByCondition(elecLabelSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }
    /**
     * 导出标签列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(ElecLabelSearch elecLabelSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(elecLabelSearch);
            PageInfo<RcLabelVo> pageInfo = elecLabelService.listPageByCondition(elecLabelSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("标签列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcLabelVo.class).sheet("标签列表").doWrite(pageInfo.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出标签列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 跳转增加桩群
     *
     * @return
     */
    @RequestMapping(value = "/addLabel", method = RequestMethod.GET)
    public String addStation(Model model) {
        return "/templates/elec/addLabel";
    }

    /**
     * 保存编辑桩群
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateElecLabel", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateElecStation(RcLabel rcLabel){

        try {
            RcLabel info = elecLabelService.selectByPrimaryKey(rcLabel.getLabelId());
            if (info!=null){
                info.setLabelName(rcLabel.getLabelName());
                info.setSort(rcLabel.getSort());
                info.setStatus(rcLabel.getStatus());
                info.setLastUpdatedTime(new Date());
                elecLabelService.updateByPrimaryKey(info);
            }else{
                rcLabel.setCreateTime(new Date());
                rcLabel.setStatus("1");
                elecLabelService.saveLabel(rcLabel);
            }
            return TResponse.SUCCESS();
        }catch (Exception e){
            logger.error("保存标签失败", e);
            return TResponse.FAIL("保存标签失败");
        }
    }

    /**
     * 跳转编辑桩群
     *
     * @return
     */
    @RequestMapping(value = "/editLabel/{labelId}", method = RequestMethod.GET)
    public String editLabel(@PathVariable("labelId") Long labelId, Model model) {
        RcLabel info = elecLabelService.selectByPrimaryKey(labelId);
        model.addAttribute("elecLabel", info);
        return "/templates/elec/editElecLabel";
    }

    /**
     * 保存编辑桩群电价
     *
     * @return
     */
    @RequestMapping(value = "/deleteElecLabel", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteElecLabel(Long labelId) {
        RcLabel info = elecLabelService.selectByPrimaryKey(labelId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        elecLabelService.deleteByPrimaryKey(labelId);
        return TResponse.SUCCESS();
    }

    /**
     * 获取所有标签
     *
     * @return
     */
    @RequestMapping(value = "/getLabels", method = RequestMethod.GET)
    @ResponseBody
    public List<RcLabel> getLabels() {
        RcLabelExample example = new RcLabelExample();
        return elecLabelService.selectByExample(example);
    }

    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public TResponse updateStatus(Long id, String status) {
        RcLabel info = elecLabelService.selectByPrimaryKey(id);
        info.setStatus(status);
        elecLabelService.updateByPrimaryKeySelective(info);
        return TResponse.SUCCESS();
    }

}
